package com.hdyg.csc.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.widget.Toast;

import com.hdyg.csc.R;
import com.hdyg.csc.activity.common.BaseActivity;
import com.hdyg.csc.app.MvpApplication;
import com.hdyg.csc.mvp.base.BasePresenter;
import com.hdyg.csc.ui.CustomProgressDialog;
import com.hdyg.csc.util.AlertDialogUtil;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 欢迎页
 *
 * @author 陈白衣
 * @time 2018/2/27 10:20
 */
public class WelcomeAty extends BaseActivity
        implements EasyPermissions.PermissionCallbacks {

    private long mExitTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        // 动态权限申请
        requireSomePermission();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected CustomProgressDialog createProgress() {
        return null;
    }

    /**
     * 有权限时执行下一步
     */
    private void nextDoing() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Bundle bundle = new Bundle();
                bundle.putString("url", "http://www.yungoulian365.com/");
//                bundle.putString("url", "https://m.youku.com/video/id_XMzY2NjM0NTI2NA==.html?spm=a2h0k.11417342.soresults.dtitle&source=&ishttps=1");
                startActivity(WebActivity.class, bundle);
                WelcomeAty.this.finish();
            }
        }, 2000);
    }

    //TODO 双击退出app
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // TODO: 2016/8/19  二次返回退出
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                toastNotifyShort("再按一次真的会退出了哦~");
                mExitTime = System.currentTimeMillis();
            } else {
                MvpApplication.getContext().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**************************权限适配****************************/
    private static final int num = 123; // 用于验证获取的权

    /**
     * 动态申请权限
     */
    @AfterPermissionGranted(num)
    private void requireSomePermission() {
        String[] perms = {
                Manifest.permission.CAMERA//相机权限
        };

        if (EasyPermissions.hasPermissions(mContext, perms)) {
            // 已有权限，进行下一步
            nextDoing();
        } else {
            // Do not have permissions, request them now （没有权限，申请权限）
            EasyPermissions.requestPermissions((Activity) mContext, "SD卡读写权限申请", num, perms);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Toast.makeText(mContext.getApplicationContext(), "授权成功！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Toast.makeText(mContext.getApplicationContext(), "授权失败！", Toast.LENGTH_SHORT).show();
        AlertDialogUtil.getInstance().showDialog(mContext, "温馨提示", "是否前往系统设置开启权限？",
                new AlertDialogUtil.OnClickYesListener() {
                    @Override
                    public void onClickYes() {
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.MAIN");
                        intent.setClassName("com.android.settings", "com.android.settings.ManageApplications");
                        startActivity(intent);
                        MvpApplication.getContext().exit();
                    }
                }, new AlertDialogUtil.OnClickNoListener() {
                    @Override
                    public void onClickNo() {
                        MvpApplication.getContext().exit();
                    }
                }, false);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 注意：this
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
